import org.junit.Test;

import java.io.*;
import java.nio.channels.FileChannel;

public class CopyFileTest {

    @Test
    public void copyFile() throws IOException {
        File srcFile = new File("D:\\JavaBase\\学习视频\\学习视频.zip");
        File dirFile = new File("E:\\2.zip");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(dirFile));
        byte[] bufs = new byte[1024];
        int length = bufferedInputStream.read(bufs);
        while (length!=-1){
            bufferedOutputStream.write(bufs);
            length = bufferedInputStream.read(bufs);
        }
        bufferedInputStream.close();
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
    }
    @Test
    public void nioCopyFile() throws IOException {
        File srcFile = new File("D:\\JavaBase\\学习视频\\学习视频.zip");
        File dirFile = new File("E:\\3.zip");
        FileChannel srcChannel = new FileInputStream(srcFile).getChannel();
        FileChannel dirChannel = new FileOutputStream(dirFile).getChannel();
        srcChannel.transferTo(0,srcChannel.size(),dirChannel);
        srcChannel.close();
        dirChannel.force(true);
        dirChannel.close();
    }
}
